package cn.yitun.chat.yitunserverimapicore.business.group.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupHeadDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupHeadInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupHeadVo;
import cn.yitun.chat.yitunservercommonlib.enums.system.UserHeadTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupHeadService;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupDao;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupHeadDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.group.manager.GroupMemberManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.group.GroupInfoPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Date 2019-01-21 14:15:31<br>
 * Description
 * @author yitun
 * @since 1.0.0
 */
@Service
@Transactional
public class GroupHeadServiceImpl
		extends CrudServiceImpl<GroupHeadDao,GroupHeadDto, GroupHeadInfo, GroupHeadVo>
		implements GroupHeadService {

	@Resource
	private GroupDao groupDAO;
	@Resource
	private GroupMemberManager groupMemberManager;
	@Resource
	private GroupInfoPush groupInfoPush;

	/**
	 * 如果已存在头像，则先删除
	 * @param userId
	 * @param groupHead
	 * @return
	 */
	public ResultMessage uploadHead(Long userId, GroupHeadDto groupHead) {
		ResultMessage message = new ResultMessage();
		try {

			if (dao.count(R.param("groupId", groupHead.getGroupId())) > 0)
				dao.removeByGroupId(groupHead.getGroupId());

			groupHead.setCreateBy(userId);
			groupHead.setUpdateBy(userId);

			dao.save(groupHead);

			Long headId = groupHead.getHeadId();
			Long groupId = groupHead.getGroupId();
			String type = groupHead.getType();
			String url = groupHead.getUrl();

			String inviterUserPosition = groupMemberManager.getPosition(groupId, userId);
			boolean isOwner = groupMemberManager.isOwner(inviterUserPosition);
			if (!isOwner) {
				return message;
			}
			// 用户自定义头像，则清除系统头像
			if (UserHeadTypeEnums.TYPE_CUSTOM.getType().equals(type)) {
				groupDAO.updateAvatar(groupId, url);
				// groupDAO.updateHead(groupId, "");
			} else {
				groupDAO.updateHead(groupId, headId);
				groupDAO.updateAvatar(groupId, "");
			}
			message.bodyPut("groupHead", groupHead);
			List<Long> userIds = groupMemberManager.getGroupMemberUserIdList(groupId);
			groupInfoPush.pushUpdateHead(IdGen.getNumber(), groupId, userIds);
		} catch (Exception e) {
			message.addWarning(ExceptionEnum.SOCK_GROUP_HEAD_UPLOAD_FAILED);
		}
		return message;
	}

	/**
	 * 获取群头像
	 * @param groupId
	 * @return
	 */
	public GroupHeadVo getGroupHeadByGroupId(Long groupId) {
		return dao.getVOByGroupId(groupId);
	}

	public List<GroupHeadVo> getGroupHeadListByGroupIds(List<Long> ids) {
		return dao.getGroupHeadListByGroupIds(ids);
	}
}
